Chapter 10 - Position and Momentum

We can start using sympy to handle symbolic math (integrals and other calculus):


In [14]:
from sympy import *

In [15]:
init_printing(use_unicode=True)

In [16]:
x, y, z = symbols('x y z', real=True)
a, c = symbols('a c', nonzero=True, real=True)

In [17]:
integrate?

There are two ways to use the integrate function. In one line, like integrate(x,(x,0,1)) or by naming an expression and then integrating it over a range:

A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a),conds='none')

We'll use both, at different times. For longer expressions, the second form can be easier to read and write.

First, just try the following, then we'll re-create some examples in the book.


In [58]:
integrate(x,(x,0,1))


Out[58]:
$$\frac{1}{2}$$

In [59]:
integrate(x**2,(x,0,1))


Out[59]:
$$\frac{1}{3}$$

The cell below will return an odd set of conditions on the result. This is because the solver doesn't want to assume anything about a and there is a special case where the answer would be different. If you look closely though, that special case isn't physically realistic so to igore these special conditions, we add conds='none'. The next cell down does what you'd expect. From here on out, just add this to the integrate function and we'll get what we expect.


In [64]:
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a))


Out[64]:
$$- c^{2} \begin{cases} - a & \text{for}\: \frac{0.5 \pi}{a} = 0 \\- 0.5 a & \text{otherwise} \end{cases} + c^{2} \begin{cases} a & \text{for}\: \frac{0.5 \pi}{a} = 0 \\0.5 a & \text{otherwise} \end{cases}$$

In [65]:
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a), conds='none')


Out[65]:
$$1.0 a c^{2}$$

So this tells us the normalization constant should be $c=\frac{1}{\sqrt{a}}$. Check that it is normalized if we do that:


In [68]:
psi = 1/sqrt(a)*cos((pi*x)/(2.0*a))  # notice we can name the expression something useful.
B = psi**2
B.integrate( (x,-a,a), conds='none')


Out[68]:
$$1.0$$

Because psi is a real function, we can calculate expectation values by integrating over $x$ or $x^2$ with psi**2:


In [70]:
C = x*psi**2
C.integrate( (x,-a,a), conds='none')


Out[70]:
$$0$$

In [71]:
D = x**2 * psi**2
E = D.integrate( (x,-a,a), conds='none')

In [73]:
E.n()  # the .n() method approximates the numerical part. You can look at the full expression below.


Out[73]:
$$0.130690966048658 a^{2}$$

In [74]:
E


Out[74]:
$$- \frac{1}{a} \left(- 0.166666666666667 a^{3} + \frac{1.0 a^{3}}{\pi^{2}}\right) + \frac{1}{a} \left(- \frac{1.0 a^{3}}{\pi^{2}} + 0.166666666666667 a^{3}\right)$$

Example 10.2


In [44]:
h = Symbol('hbar', real=True)

Use the diff function to take a derivative of a symbolic expression. For example:


In [75]:
diff(x**2, x)


Out[75]:
$$2 x$$

In [45]:
# Solution goes here

In [28]:
# Solution goes here

Example 10.3


In [29]:
p = Symbol('p', real=True)

In [42]:
# Solution goes here

In [43]:
# Solution goes here

In [ ]: